//1. 获取页面元素对象
//获取用户名
let $Name = $('#txtUserName');
//获取密码
let $Pwd = $('#txtPass');

//注册按钮
let $Sub = $('.registor');
// console.log($Sub);

//获取登录按钮
let $Go = $('#btnLogin')	
//给登录按钮添加点击事件

$Go.click(function(){
	//获取用户名
	let uname = $Name.val();
	//获取密码
	let upwd = $Pwd.val();
	//判断用户名是否为空
	if(!uname){
		alert('用户名不能为空');
		return;
	}
	// 获取现有的cookie
	let cookieStr = $.cookie('registors') ? $.cookie('registors') : '';
	//转为对象
	let cookieObj = convertCookieStrToCookieObj(cookieStr);
	if(uname in cookieObj){
        if(cookieObj[uname] === upwd){
            location.href = 'http://localhost:8090/huijiayou/dist/index.html';
        }else{
		alert('请确认您的用户名或密码是否正确');
	    }  
	}else{
        alert('用户名不存在！');
    }
})
//给注册按钮添加点击事件
$Sub.click(function(){
	location.href = 'http://localhost:8090/huijiayou/dist/pages/registor.html';
})
// 将cookie字符串转为cookie对象
function convertCookieStrToCookieObj(str){
	if(!str){
		return {};
	}
	return JSON.parse(str);
}

//搜索栏下
$.ajax({
    type: 'get',
    url: '../../json/search.json',
    async: true,
    success: function (data) {
        var searchArr = data.search;
        for (var i = 0; i < searchArr.length; i++) {
            var node = $(`<a href="" class="curr">${searchArr[i].name}</a>`);
            node.appendTo(".search #hotWord");

        }
    }
})
